﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.ComponentModel;
using DTC.Database;

namespace DTC.Popups
{
    /// <summary>
    /// Interaction logic for PatientDrugPrescriptionListPopup.xaml
    /// </summary>
    public partial class PatientDrugPrescriptionListPopup : Window
    {
        public PatientDrugPrescriptionListPopup()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            
        }

        private DTCPatient patient;
        public DTCPatient Patient
        {
            get
            {
                return patient;
            }
            set
            {
                if (patient != value)
                {
                    patient = value;
                    prescriptions = new ObservableCollection<DTCDrugPrescription>(App.DBI.GetAllPatientPrescriptions(patient.PatientID));
                    issues = new ObservableCollection<DTCDrugIssue>(App.DBI.GetAllPatienIssues(patient.PatientID));
                    DrugPrescriptionsListBox.ItemsSource = prescriptions;
                    DrugIssuesListBox.ItemsSource = issues;
                }
            }
        }

        private ObservableCollection<DTCDrugPrescription> prescriptions;
        private ObservableCollection<DTCDrugIssue> issues;

        private void Window_Closing(object sender, CancelEventArgs e)
        {

        }

        private void NewDrugIssueButton_Click(object sender, RoutedEventArgs e)
        {
            DrugIssuePopup dip = new DrugIssuePopup();
            dip.Owner = this;
            if (dip.ShowDialog() == true)
            {
                //issues.Add(dip.Issue);
                //DrugIssuesListBox.ItemsSource = null;
                //DrugIssuesListBox.ItemsSource = issues;
            }
        }

        private void NewPrescriptionButton_Click(object sender, RoutedEventArgs e)
        {
            DrugPrescriptionPopup dpp = new DrugPrescriptionPopup();
            dpp.Patient = patient;
            dpp.Owner = this;
            if (dpp.ShowDialog() == true)
            {
                prescriptions.Add(dpp.DrugPrescription);
                DrugPrescriptionsListBox.ItemsSource = null;
                DrugPrescriptionsListBox.ItemsSource = prescriptions;
            }
        }

        private void DeletePresButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DTCDrugPrescription selectedPrescription = (DTCDrugPrescription)cmd.Tag;
            if(selectedPrescription.CanBeDeleted)
            {
                ConfirmationPopup cp = new ConfirmationPopup();
                cp.Message =
                    string.Format("Вы уверены в том, что хотите удалить выбранное назначение?");
                if (cp.ShowDialog() == true)
                {
                    prescriptions.Remove(selectedPrescription);
                    List<DTCOrder> AffectedOrders = App.DBI.GetOrdersByTime(selectedPrescription.CourseStartDate.Value,
                                                                            selectedPrescription.CourseStartDate.Value.
                                                                                AddDays(
                                                                                selectedPrescription.CourseDurationDays.
                                                                                    Value));
                    foreach (DTCOrder o in AffectedOrders)
                    {
                        o.PatientOrderStatuse = App.DBI.GetStatusByID((int) DTCOrder.OrderStatuses.Changed);
                    }
                    selectedPrescription.Delete();
                }
            }
            else
            {
                WarningPopup wp = new WarningPopup();
                wp.Message = "Назначение не можеыт быть удаленно, так как оно учавствует в ранее созданых заказах";
                wp.Show();
            }
        }

        private void ViewPrescButton_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DTCDrugPrescription selectedPrescription = (DTCDrugPrescription)cmd.Tag;
            DrugPrescriptionPopup dpp = new DrugPrescriptionPopup();
            dpp.DrugPrescription = selectedPrescription;
            dpp.Owner = this;
            dpp.ShowDialog();
        }

        private void CalculateNeed_Click(object sender, RoutedEventArgs e)
        {
            Button cmd = (Button)sender;
            DTCDrugPrescription selectedPrescription = (DTCDrugPrescription)cmd.Tag;
            DrugIssuePopup dip = new DrugIssuePopup();
            dip.Owner = this;
            dip.DrugPrescription = selectedPrescription;
            dip.ShowDialog();
        }
    }
}
